﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DALLayer.Base;
using DataDefinition;

namespace DALLayer.Providers
{
    public class StepDataProvider: DataAccessBase
    {
        #region [Constructor]
        /// <summary>
        /// Initializes a new instance of the <see cref="StepDataProvider" /> class.
        /// </summary>
        public StepDataProvider(){ }
        #endregion

        #region [Public Methods]

        public AutoFixDataSet GetAllStepsByArticleID(int articleId)
        {
            AutoFixDataSet dataSet = null;

            try
            {
                //Triggers the method in the parent class DataAccessBase
                base.InitializeDataAccess();
                //Passes the sql string that needs to be added to the property in the parent class
                base.CommandTextBuilder.Append(@"SELECT StepName, StepURL 
                                                FROM Step
                                                WHERE (ArticleID = @ArticleID)");
              
                //Add the sql string to retrieve data based on ArticleID
                base.CommandTextBuilder.Append(@" WHERE (ArticleID = @ArticleID)");
                //Add the params needed to retrieve data
                base.DBParams.Add(@"@ArticleID", articleId);

                bool success = false;
                //Check if it is success
                success = base.ExecuteReader();
                //If it is successful
                if (success == true)
                {
                    //Initial the dataset
                    dataSet = new AutoFixDataSet();
                    //Fill the table
                    dataSet.Article.Load(base.DataReader);
                }
            }
            catch (Exception eX)
            {
                // log the error here
            }
            finally
            {
                //Close connection
                base.FinalizeConnection();
            }
            //Return the filled dataset
            return dataSet;
        }
        /// <summary>
        /// Gets all step or by id
        /// </summary>
        /// <param name="stepId">step id</param>
        /// <param name="selectById">check whether select by id</param>
        /// <returns>dataset</returns>
        public AutoFixDataSet GetAllSteps(int stepId, bool selectById)
        {
            AutoFixDataSet dataSet = null;
            try
            {
                //Triggers the method in the parent class DataAccessBase
                base.InitializeDataAccess();
                //Passes the sql string that needs to be added to the property in the parent class
                base.CommandTextBuilder.Append(@"SELECT Step.* FROM Step");
                //if its a selection by id
                if (selectById == true)
                {
                    //Add the sql string to retrieve data based on ArticleTypeID
                    base.CommandTextBuilder.Append(@" WHERE (StepID = @StepID)");
                    //Add the params needed to retrieve data
                    base.DBParams.Add(@"@StepID", stepId);
                }
                bool success = false;
                //Check if it is success
                success = base.ExecuteReader();
                //If it is successful
                if (success == true)
                {
                    //Initial the dataset
                    dataSet = new AutoFixDataSet();
                    //Fill the table
                    dataSet.Step.Load(base.DataReader);
                }
            }
            catch (Exception eX)
            {
                // log the error here
            }
            finally
            {
                //Close connection
                base.FinalizeConnection();
            }
            //Return the filled dataset
            return dataSet;
        }
        /// <summary>
        /// Gets the steps by user id or article id
        /// </summary>
        /// <param name="isArticleId">check to see if its article id if<value>true</value> then article id else<value>false</value> user id</param>
        /// <param name="articleId">the article id</param>
        /// <param name="userId">the user id</param>
        /// <returns>dataset</returns>
        public AutoFixDataSet GetStepsUsersOrArticles(bool isArticleId, int articleId, int userId)
        {
            AutoFixDataSet dataSet = null;

            try
            {
                //Triggers the method in the parent class DataAccessBase
                base.InitializeDataAccess();
                //Passes the sql string that needs to be added to the property in the parent class
                base.CommandTextBuilder.Append(@"SELECT Step.StepName, Step.StepID, Step.StepURL, Article.IsPublished");
                base.CommandTextBuilder.Append(@" FROM ((ApplicationUser INNER JOIN
                         Article ON ApplicationUser.UserID = Article.UserID) INNER JOIN
                         Step ON Article.ArticleID = Step.ArticleID)");
                if (isArticleId)
                {
                    //Add the sql string to retrieve data based on ArticleID
                    base.CommandTextBuilder.Append(@" WHERE (Article.ArticleID = @ArticleID)");
                    //Add the params needed to retrieve data
                    base.DBParams.Add(@"@ArticleID", articleId);
                }
                else
                {
                    //Add the sql string to retrieve data based on ArticleID
                    base.CommandTextBuilder.Append(@" WHERE (Article.UserID = @UserID)");
                    //Add the params needed to retrieve data
                    base.DBParams.Add(@"@UserID", userId);
                }
                base.CommandTextBuilder.Append(@"AND (Article.IsActive = True) AND (ApplicationUser.IsActive = True) AND (Step.IsActive = True)");


                bool success = false;
                //Check if it is success
                success = base.ExecuteReader();
                //If it is successful
                if (success == true)
                {
                    //Initial the dataset
                    dataSet = new AutoFixDataSet();
                    //Fill the table
                    dataSet.GetStepsUsersOrArticles.Load(base.DataReader);
                }
            }
            catch (Exception eX)
            {
                // log the error here
            }
            finally
            {
                //Close connection
                base.FinalizeConnection();
            }
            //Return the filled dataset
            return dataSet;
        }

        /// <summary>
        /// The method inserts step to the table
        /// </summary>
        /// <param name="stepName">step name</param>
        /// <param name="stepUrl">step url</param>
        /// <param name="articleId">article id</param>
        /// <param name="isActive">is active</param>
        /// <returns>true or false</returns>
        public bool InsertStep(string stepName, string stepUrl, int articleId, bool isActive)
        {

            bool success = false;
            try
            {
                //Triggers the method in the parent class DataAccessBase
                base.InitializeDataAccess();
                //Passes the sql string that needs to be added to the property in the parent class

                base.CommandTextBuilder.Append(@"
                    INSERT INTO Step(StepName, StepURL, ArticleID, IsActive)
                    VALUES(@StepName, @StepURL, @ArticleID, @IsActive)");
                //Add the params needed to retrieve data
                base.DBParams.Add(@"@StepName", stepName);
                base.DBParams.Add(@"@StepURL", stepUrl);
                base.DBParams.Add(@"@ArticleID", articleId);
                base.DBParams.Add(@"@IsActive", isActive);

                //Check if it is success
                success = base.ExecuteNonQuery();

            }
            catch (Exception)
            {
                success = false;
                //Log here
            }
            //return success
            return success;
        }

        /// <summary>
        /// The method updates step
        /// </summary>
        /// <param name="stepId">step id</param>
        /// <param name="stepName">step name</param>
        /// <param name="stepUrl">step url</param>
        /// <param name="articleId">article id</param>
        /// <param name="isActive">is active</param>
        /// <returns>true or false</returns>
        public bool UpdateStep(int stepId, string stepName, string stepUrl, int articleId, bool isActive)
        {
            bool success = false;
            try
            {
                //Triggers the method in the parent class DataAccessBase
                base.InitializeDataAccess();
                //Passes the sql string that needs to be added to the property in the parent class

                base.CommandTextBuilder.Append(@"
                    UPDATE Step
                    SET StepName = @StepName, 
                        StepURL =  @StepURL, 
                        ArticleID = @ArticleID,                        
                        IsActive = @IsActive
                    WHERE StepID = @StepID");
                //Add the params needed to retrieve data
                base.DBParams.Add(@"@StepName", stepName);
                base.DBParams.Add(@"@StepURL", stepUrl);
                base.DBParams.Add(@"@ArticleID", articleId);
                base.DBParams.Add(@"@IsActive", isActive);
                base.DBParams.Add(@"@StepID", stepId);

                //Check if it is success
                success = base.ExecuteNonQuery();

            }
            catch (Exception)
            {
                success = false;
                //Log here
            }
            //return success
            return success;

        }

        
        #endregion
    }
}
